import Vue from 'vue'
import Vuex from 'vuex'
import axios from "axios"

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    scollnav: [],//滑动格
    goodslist: [],//商品列表
    details: [],//详情全部
    pageCategoryList: [],//左侧
    categoryList: [],//右侧
   
    cart: [],//购物车商品列表 
    total: 0,//总价
    allChecked: false, //购物车底部的复选框
    isShow: false,
  },
  mutations: {
    //滑动格
    getScollNav(state, items) {
      state.scollnav = items
    },
    //商品列表
    getGoodsList(state, items) {
      state.goodslist = items
    },
    //详情全部
    getDetails(state, item) {
      state.details = item
      state.pageCategoryList = item.pageCategoryList;
      state.categoryList = item.categoryList;
      console.log(item.pageCategoryList);
      console.log(item.categoryList)
    },
    //添加购物车
    add_cart(state, item) {
      state.isShow = false;
      let flag = false;
      //判断添加商品是否重复
      state.cart.map((ele) => {
        // 重复数量加一
        if (ele.item.spuId == item.spuId) {
          flag = true;
          ele.count++;
        }
      })
      //否则单个添加
      if (!flag) {
        state.cart.push({
          item,
          count: 1,
          checked: false
        })
      }
      state.total = 0;
      state.cart.map(ele => {
        if (ele.checked) {
          state.total += ele.count * ele.item.currentPrice
        }
      })
      localStorage.setItem('cart', JSON.stringify(state.cart))
    },
    // 购物车内商品数量减
    SUB_DATA(state, item) {
      if (item.count > 1) {
        item.count--;
        state.total = 0;
        state.cart.map(ele => {
          if (ele.checked) {
            state.total += ele.count * ele.item.currentPrice
          }
        })
        localStorage.setItem('cart', JSON.stringify(state.cart))
      }
    },
    // 购物车内商品数量加
    ADD_DATA(state, item) {
      item.count++;
      state.total = 0;
      state.cart.map(ele => {
        if (ele.checked) {
          state.total += ele.count * ele.item.currentPrice
        }
      })
      localStorage.setItem('cart', JSON.stringify(state.cart))
    },
    // 购物车内删除商品
    DELETE_DATA(state, index) {
      state.cart.splice(index, 1);
      if (state.cart.length == 0) {
        state.isShow = true;
      } else {
        state.isShow = false;
      }
      state.total = 0;
      state.cart.map(ele => {
        if (ele.checked) {
          state.total += ele.count * ele.item.currentPrice
        }
      })
      localStorage.setItem('cart', JSON.stringify(state.cart))
    },
    // 点击单个商品上面的复选框
    CHANGECHECKED(state, item) {
      item.checked = !item.checked;
      var status = state.cart.every(el => {
        return el.checked == true
      })
      state.allChecked = status;
      state.total = 0;
      state.cart.map(ele => {
        if (ele.checked) {
          state.total += ele.count * ele.item.currentPrice
        }
      })
      localStorage.setItem('cart', JSON.stringify(state.cart))
    },
    // 全选的复选框
    ChECKALL(state) {
      state.allChecked = !state.allChecked;
      state.cart.forEach(ele => {
        ele.checked = state.allChecked
      });
      state.total = 0;
      state.cart.map(ele => {
        if (ele.checked) {
          state.total += ele.count * ele.item.currentPrice
        }
      })
      localStorage.setItem('cart', JSON.stringify(state.cart))
    }
  },
  actions: {
    //滑动格
    getScollNav(context) {
      axios.get("./api/category.json")
        .then(res => {
          // console.log(res.data.data.kingkongList)
          context.commit('getScollNav', res.data.data.kingkongList)
        }).catch(err => {
          console.error(err)
        })
    },
    //商品列表
    getGoodsList(context) {
      axios.get("./api/listData.json")
        .then(res => {
          // console.log(res.data.data.shopList)
          context.commit('getGoodsList', res.data.data.shopList)
        })
    },
    //详情全部
    getDetails(context, id) {
      axios.get("./api/shopdetails.json", { params: { id: id } })
        .then(res => {
          console.log(res.data)
          res.data.forEach(ele => {
            if (ele.data.mtWmPoiId === id) {
              context.commit('getDetails', ele.data)
            }
          })
        })
    },
    // 添加购物车
    add_cart(context, item) {
      context.commit('add_cart', item)
    },
    // 减数量
    SUB_DATA(context, item) {
      context.commit('SUB_DATA', item)
    },
    // 加数量
    ADD_DATA(context, item) {
      context.commit('ADD_DATA', item)
    },
    // 删除商品
    DELETE_DATA(context, index) {
      context.commit('DELETE_DATA', index)
    },
    // 点击商品上面的复选框
    CHANGECHECKED(context, item) {
      context.commit('CHANGECHECKED', item)
    },
    // 全选的复选框
    ChECKALL(context) {
      context.commit("ChECKALL")
    }
  },
  modules: {
  }
})
